package com.jdz.gxq.info.admin.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.jdz.gxq.info.admin.project.bo.TalkingProject;
import com.jdz.gxq.info.admin.project.form.TalkingProjectForm;
import com.jdz.gxq.info.admin.project.service.TalkingProjectService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class TalkingProjectAction extends CommonDispatchAction {

	private TalkingProjectService talkingProjectService = null;
	 
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		TalkingProjectForm talkingProjectForm = (TalkingProjectForm) form;
		
		String projectName = talkingProjectForm.getProjectName();

		int totalCount = talkingProjectService.getTalkingProjectsCount(projectName);
		
		int currentPage = talkingProjectForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);
		
		talkingProjectForm.setPageIndex(pageBean.getCurrentPage());
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());
		params.put("projectName", projectName);
		
		List<TalkingProject> talkingProjectList = talkingProjectService.getTalkingProjects(params);
		
		request.setAttribute("talkingProjectList", talkingProjectList);
		request.setAttribute("pageBean", pageBean);
		
		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		TalkingProjectForm talkingProjectForm = (TalkingProjectForm) form;
		
		TalkingProject talkingProject = talkingProjectForm.getTalkingProject();

		// talkingProject名重复验证(其他后台逻辑验证也类似处理)
		if (!talkingProjectService.checkTalkingProjectName(talkingProject, 1)) {
			ActionMessages messages = new ActionMessages();
			messages.add("talkingProject.projectName", new ActionMessage("error.projectNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}
		
		talkingProject.setModifiedBy(getUserId(request));
		
		talkingProjectService.insertTalkingProject(talkingProject);

		return mapping.findForward("success");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String projectIdsStr = request.getParameter("id");
		
		String[] projectIds = projectIdsStr.split(",");
		
		for (String projectId : projectIds) {
			talkingProjectService.deleteTalkingProject(Integer.parseInt(projectId));
		}

		return mapping.findForward("success");
	}
	
	public ActionForward gotoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		TalkingProjectForm talkingProjectForm = (TalkingProjectForm) form;
	
		TalkingProject talkingProject = talkingProjectService.getTalkingProjectById(talkingProjectForm.getId());
		
		talkingProjectForm.setTalkingProject(talkingProject);

		return mapping.findForward("success");
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		TalkingProjectForm talkingProjectForm = (TalkingProjectForm) form;
		
		TalkingProject talkingProject = talkingProjectForm.getTalkingProject();
		
		// talkingProject名重复验证(其他后台逻辑验证也类似处理)
		if (!talkingProjectService.checkTalkingProjectName(talkingProject, 2)) {
			ActionMessages messages = new ActionMessages();
			messages.add("talkingProject.projectName", new ActionMessage("error.projectNameExisted"));
			addErrors(request, messages);
			return mapping.getInputForward();
		}

		talkingProject.setModifiedBy(getUserId(request));
		
		talkingProjectService.updateTalkingProject(talkingProject);
		
		return mapping.findForward("success");
	}

	public void setTalkingProjectService(TalkingProjectService talkingProjectService) {
		this.talkingProjectService = talkingProjectService;
	}
}

